﻿using Engine;
using Engine.Graphics;
using Game.NetWork;
using Game.NetWork.Packages;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentHealBuffA : Component, IUpdateable
    {
        public BuffParticleSystem onBuffParticleSystem;

        public SubsystemGameInfo m_subsystemGameInfo;

        public SubsystemTerrain m_subsystemTerrain;

        public ComponentCreature m_componentCreature;

        public SubsystemTime m_subsystemTime;

        public SubsystemAudio m_subsystemAudio;

        public SubsystemParticles m_subsystemParticles;

        public ComponentPlayer m_componentPlayer;

        public float m_HealRate;

        public float m_HealDuration;

        public Random m_random = new Random();

        public float HealingRate => m_HealRate;

        public bool IsActive => m_HealDuration > 0f;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public void StartHealBuff(float healtime, float healrate)
        {
            if (CommonLib.WorkType != WorkType.Client)
            {
                StartHealBuffNet(healtime, healrate);
            }
        }

        public void StartHealBuffNet(float healtime, float healrate)
        {
            m_HealRate = healrate;
            m_HealDuration = healtime;
        }

        public void StopBuff()
        {
            if (CommonLib.WorkType != WorkType.Client)
            {
                CommonLib.Net.QueuePackage(new ComponentHealPackage(this));
                if (m_componentPlayer != null)
                {
                    m_HealDuration = 0f;
                    m_componentPlayer.ComponentGui.DisplaySmallMessage("生命恢复效果已停止！", Color.Red, blinking: true, playNotificationSound: false);
                }
            }
        }

        public void Update(float dt)
        {
            if (m_HealDuration <= 0f && onBuffParticleSystem != null)
            {
                onBuffParticleSystem.IsStopped = true;
                onBuffParticleSystem = null;
            }

            if (IsActive && m_HealDuration > 0f && HealingRate > 0f)
            {
                if (onBuffParticleSystem == null)
                {
                    onBuffParticleSystem = new BuffParticleSystem
                    {
                        Texture = ContentManager.Get<Texture2D>("Textures/Star")
                    };
                    m_subsystemParticles.AddParticleSystem(onBuffParticleSystem);
                }

                if (onBuffParticleSystem != null)
                {
                    BoundingBox boundingBox = m_componentCreature.ComponentBody.BoundingBox;
                    onBuffParticleSystem.Position = 0.5f * (boundingBox.Min + boundingBox.Max);
                    onBuffParticleSystem.Radius = 0.5f * MathUtils.Min(boundingBox.Max.X - boundingBox.Min.X, boundingBox.Max.Z - boundingBox.Min.Z);
                    onBuffParticleSystem.Color = Color.Lerp(Color.LightGreen, Color.Green, MathUtils.Saturate((m_HealDuration - m_componentCreature.ComponentHealth.AirCapacity) / m_HealDuration));
                    onBuffParticleSystem.Size = m_componentCreature.ComponentBody.BoxSize.XZ * 0.1f;
                }

                m_HealDuration -= dt;
                if (CommonLib.WorkType != WorkType.Client && m_componentPlayer.ComponentHealth.Health > 0f && Time.PeriodicEvent(0.5, 0.0))
                {
                    healing();
                    ComponentHealth health = base.Entity.FindComponent<ComponentHealth>();
                    CommonLib.Net.QueuePackage(new ComponentHealthPackage(health));
                    CommonLib.Net.QueuePackage(new ComponentHealPackage(this));
                }
            }
        }

        public void healing()
        {
            m_componentPlayer.ComponentHealth.Heal(1f / m_componentPlayer.ComponentHealth.AttackResilience * HealingRate * 0.5f);
        }

        public override void OnEntityRemoved()
        {
            if (onBuffParticleSystem != null)
            {
                onBuffParticleSystem.IsStopped = true;
                onBuffParticleSystem = null;
            }
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
            m_subsystemParticles = base.Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            m_componentCreature = base.Entity.FindComponent<ComponentCreature>(throwOnError: true);
            m_componentPlayer = m_componentCreature.Entity.FindComponent<ComponentPlayer>();
            m_HealDuration = valuesDictionary.GetValue("HealDuration", 0f);
            m_HealRate = valuesDictionary.GetValue("HealRate", 1f);
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("HealDuration", m_HealDuration);
            valuesDictionary.SetValue("HealRate", m_HealRate);
        }
    }
}